package com.dorm.service;

import com.dorm.entity.Student;
import com.dorm.entity.Room;
import com.dorm.entity.Building;
import com.dorm.mapper.StudentMapper;
import com.dorm.mapper.RoomMapper;
import com.dorm.mapper.BuildingMapper;
import com.dorm.util.Debug;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class StudentService {
    private final StudentMapper studentMapper;
    private final RoomMapper roomMapper;
    private final BuildingMapper buildingMapper;

    public StudentService(StudentMapper studentMapper, RoomMapper roomMapper, BuildingMapper buildingMapper) {
        this.studentMapper = studentMapper;
        this.roomMapper = roomMapper;
        this.buildingMapper = buildingMapper;
    }

    public List<Student> getAllStudents() {
        return studentMapper.findAll();
    }

    public Student getStudentById(Long id) {
        try {
            Student student = studentMapper.findById(id);
            if (student == null) {
                throw new RuntimeException("学生不存在");
            }
            
            // 加载关联数据
            if (student.getRoomId() != null) {
                Room room = roomMapper.findById(student.getRoomId());
                if (room != null) {
                    student.setRoom(room);
                    // 加载宿舍楼信息
                    Building building = buildingMapper.findById(room.getBuildingId());
                    if (building != null) {
                        room.setBuilding(building);
                    }
                }
            }
            
            return student;
        } catch (Exception e) {
            Debug.error("获取学生详情失败:", e);
            throw new RuntimeException("获取学生详情失败: " + e.getMessage());
        }
    }

    public List<Student> getStudentsByRoom(Long roomId) {
        return studentMapper.findByRoomId(roomId);
    }

    @Transactional
    public Student createStudent(Student student) {
        studentMapper.insert(student);
        updateRoomOccupancy(student.getRoomId(), 1);
        return student;
    }

    @Transactional
    public Student updateStudent(Student student) {
        Student oldStudent = studentMapper.findById(student.getId());
        if (oldStudent.getRoomId() != null && !oldStudent.getRoomId().equals(student.getRoomId())) {
            // 如果学生换宿舍了，需要更新两个房间的入住人数
            updateRoomOccupancy(oldStudent.getRoomId(), -1);
            updateRoomOccupancy(student.getRoomId(), 1);
        }
        studentMapper.update(student);
        return student;
    }

    @Transactional
    public void deleteStudent(Long id) {
        Student student = studentMapper.findById(id);
        if (student.getRoomId() != null) {
            updateRoomOccupancy(student.getRoomId(), -1);
        }
        studentMapper.deleteById(id);
    }

    private void updateRoomOccupancy(Long roomId, int delta) {
        if (roomId != null) {
            var room = roomMapper.findById(roomId);
            room.setCurrentOccupancy(room.getCurrentOccupancy() + delta);
            roomMapper.update(room);
        }
    }

    public Student getStudentWithDetails(Long id) {
        return studentMapper.findStudentWithDetails(id);
    }
} 